<?php
	@header("content-Type: text/html; charset=utf-8");	
	require_once "inc/pigm.function.php";
	require_once "inc/xmldb.class.php";
	require_once "inc/runtimer.class.php";
	require_once "inc/db.config.php";
	
	//实例化xml数据库类
	$tagsDB = new xmlDB($DBset_tags);
	$musicDB = new xmlDB($DBset_music);
	
	//运行时间计数
	$runtime = new runtimer();
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pig Flash Music Player</title>
<link href="inc/style.css" rel="stylesheet" type="text/css" />
<script src="inc/jquery.js" type="text/javascript" language="javascript"></script>
<script src="inc/admin.js" type="text/javascript" language="javascript"></script>
</head>
<body>


<?php
	$m = $_GET['m'];
	$act = $_GET['act'];
	if(empty($m ))$m  = "music";
?>


<div class="page">
	<div class="act">
		<ul>
			<li><a href="?m=music" <?php if($m=="music" || $m=="edit")echo "class=\"thisclass\"";?>>音乐管理</a></li>
			<li><a href="?m=upmusic" <?php if($m=="upmusic")echo "class=\"thisclass\"";?>>添加音乐</a></li>
			<li><a href="?m=tags" <?php if($m=="tags")echo "class=\"thisclass\"";?>>标签管理</a></li>
		</ul>
	</div>
	<div class="main">
<?php
	if($m  =="music"){//音乐管理
		if($act == "update"){
			$id = $_POST['id'];
			$music = trim($_POST['music']);
			$author = trim($_POST['author']);
			$special = trim($_POST['special']);
			$url = trim(urlencode($_POST['url']));
			$intro = trim($_POST['intro']);
			$lrc = trim($_POST['lrc']);
			$tags = trim($_POST['tags']);
			//验证数据
			$isindb = $musicDB->GetList('/<music><\!\[CDATA\['.$music.'\]\].+?<author><\!\[\CDATA\['.$author.'\]\].+?<url><\!\[CDATA\['.$url.'\]\].+?/s');
			//dump($isindb);
			//dump($musicDB->listcount);
			//dump($_POST);
			if($id=="add" && $musicDB->listcount<1){//添加
				//构造数组
				$madd = array(
					'music'		=>	$music,
					'author'	=>	$author,
					'special'	=>	$special,
					'url'		=>	$url,
					'intro'		=>	$intro,
					'lrc'		=>	$lrc,
					'tags'		=>	$tags
				);
				$musicDB->Insert($madd);
				msg("添加音乐成功","成功");				
			}else if(is_numeric($id) && $isindb[$id]['id']!=$id){//修改
				//构造数组
				$mup = array(
					'id'		=>	$id,
					'music'		=>	$music,
					'author'	=>	$author,
					'special'	=>	$special,
					'url'		=>	$url,
					'intro'		=>	$intro,
					'lrc'		=>	$lrc,
					'tags'		=>	$tags
				);
				$musicDB->update($mup);
				msg("修改音乐成功","成功");				
			}else{//其他
				msg("检测到数据重复,请检查数据","重复");
			}
			redirect("?m=music",1);
		}else if(is_numeric($_GET['del'])){
			$musicDB->Del($_GET['del']);
			msg("删除数据成功","成功");
			redirect("?m=music");
		}else{
?>
		<dl>
			<dt onclick="plreload();">音乐列表</dt>
			<dd>
				<script language="javascript" type="text/javascript" >
				<!--
					$(function(){
						window.parent.window.frames['player'].GetList();
						$(".plist").find("tr td a[_mid]").click(function(){
							window.parent.window.frames['player'].PlayM($(this));
						});
					});
				-->
				</script>
				<table class="plist">
					<tbody>
						<tr>
							<th width="140">歌曲</td>
							<th width="60">歌手</td>
							<th width="38">操作</td>
						</tr>
					<?php
						$musicDB->pagesize = 10;
						$page = $_GET['p'];
						foreach ($musicDB->GetList("",$page) as $k=>$v){
					?>
						<tr>
							<td>
								<a href="javascript:void(null);" _mid="<?php echo $v['id'];?>"><?php echo $v['music'];?></a><br />
							</td>
							<td>
								<?php echo $v['author'];?><br />
							</td>
							<td>
								<!--<a href="javascript:void(null)" class="viewmore">详细</a> | --><a href="?m=edit&id=<?php echo $k;?>">编辑</a> | <a href="?m=music&del=<?php echo $k;?>" onclick="return confirm('确定删除?');">删除</a>
							</td>
						</tr>
						<!--<tr>
							<td colspan="3" class="moreinfo">
								<div>
									<p>
										<?php echo $v['music'];?>  &nbsp; <?php echo $v['author'];?>  &nbsp;  <?php echo $v['special'];?><br />
										<?php echo urldecode($v['url']);?>
									</p>
									
									<p><?php dump($v['lrc']);?></p>
								</div>
							</td>
						</tr>-->
					<?php
						}
					?>
					</tbody>
					<tfoot>
						<tr>
							<td colspan="3" class="pagelist">
								<?php 
									$pagenum = ceil($musicDB->listcount / $musicDB->pagesize);
									echo "Page: ";
									for($p=1;$p<=$pagenum;$p++){
										echo "<a href=\"?m=tags&p=$p\">$p</a> ";
									}
									echo " Count: ".$musicDB->listcount;
								?>
							</td>
						</tr>
					</tfoot>				
				</table>
			</dd>
		</dl>


<?php
		}
	}else if($m  == "upmusic" || $m == "edit"){//添加音乐
		$id = $_GET['id'];
		if($m=="edit" && is_numeric($id)){
			$row = $musicDB->GetOneByID($id);
			$mtitle = "编辑音乐";
		}else{
			$id = "add";
			$mtitle = "添加音乐";
		}
?>			
		<dl>
			<dt><?php echo $mtitle;?></dt>
			<dd>
				<form action="?m=music&act=update" method="POST" id="upmusic">
				<input type="hidden" name="id" size="36" class="txt" value="<?php echo $id;?>" />
				<table class="pform">
					<tbody>
						<tr>
							<td class="td1" width="50">歌曲</td>
							<td><input type="text" name="music" size="36" class="txt" value="<?php echo $row['music'];?>" /></td>
						</tr>
						<tr>
							<td class="td1">作者</td>
							<td><input type="text" name="author" size="20" class="txt" value="<?php echo $row['author'];?>" /></td>
						</tr>
						<tr>
							<td class="td1">专辑</td>
							<td><input type="text" name="special" size="33" class="txt" value="<?php echo $row['special'];?>" /></td>
						</tr>
						<tr>
							<td class="td1">地址</td>
							<td><input type="text" name="url" size="56" class="txt" value="<?php echo urldecode($row['url']);?>" /><p><small>仅限MP3格式</small></p></td>
						</tr>
						<tr>
							<td class="td1">简介</td>
							<td><textarea cols="56" rows="3" class="txt" name="intro"><?php echo $row['intro'];?></textarea></td>
						</tr>
						<tr>
							<td class="td1">歌词</td>
							<td><textarea cols="56" rows="10" class="txt" name="lrc"><?php echo $row['lrc'];?></textarea></td>
						</tr>
						<tr>
							<td class="td1">标签</td>
							<td>
								<input type="text" name="tags" size="56" class="txt" value="<?php echo $row['tags'];?>" />
								<div class="tagbox">
							<?php
								foreach ($tagsDB->GetList("","all") as $k => $v){
									if(preg_match("/{".$v['tag']."}/s",$row['tags']))echo "<a href=\"javascript:void(null);\" class=\"select\">".$v['tag']."</a> ";
									else echo "<a href=\"javascript:void(null);\">".$v['tag']."</a> ";
								}
							?>
							</div>
							</td>
						</tr>
						<tr>
							<td></td>
							<td>
								<button type="submit" class="btn1">提交</button>
								<button type="button" class="btn1" onclick="location.href='?m=music';">返回</button>
							</td>
						</tr>
					</tbody>
				</table>
				</form>
			</dd>
		</dl>



<?php
	}else if($m  =="tags"){//标签管理
		
	$tag = trim($_POST['tag']);
	$edit = $_GET['edit'];
	$del = $_GET['del'];
	$id = $_POST['id'];
	
	if($act == "update"){//添加及编辑
		//判断重复
		$isindb = $tagsDB->GetList('/<tag><\!\[CDATA\['.$tag.'\].*/s');
		if($tagsDB->listcount < 1){
			if($id == "add" && !empty($tag)){//添加标签
				$tagsDB->Insert(array('tag'=>$tag));
				msg("添加标签成功","成功");
			}else if(!empty($tag) && is_numeric($id)){//修改标签
				$tagsDB->Update(array('id'=>$id,'tag'=>$tag));
				msg("修改标签成功","成功");
			}else{
				msg("出现错误,请检查后重新提交.","错误");
			}
			redirect("?m=tags");
		}else{
			msg("标签名重复,请输入其他标签.","错误");
			redirect("?m=tags",1);
		}
		
	}else if(is_numeric($del)){
		$tagsDB->Del($del);
		msg("删除标签成功","成功");
		redirect("?m=tags");
	}else{
		
		if(empty($edit)){
			$id = "add";
			$tag = "";
			$mtitle = "添加标签";
		}else{
			$mtitle = "修改标签";
			$id = $edit;
			//$row = $tagsDB->GetList('/<rows id=\"'.$edit.'\".*/s');
			//$tag = $row[$edit]['tag'];
			$row = $tagsDB->GetOneByID($id);
			$tag = $row['tag'];
			$relink = " <button type=\"button\" class=\"btn1\" onclick=\"location.href='?m=tags';\">取消</button>";
		}
?>
		<dl>
			<dt><?php echo $mtitle;?></dt>
			<dd>
				<form action="?m=tags&act=update" method="POST" id="uptags">
				<table class="pform">
					<tbody>
						<tr>
							<td>
								标签: 
								<input type="text" name="tag" size="30" class="txt" value="<?php echo $tag;?>" />
								<input type="hidden" name="id" size="30" class="txt" value="<?php echo $id;?>" />
								<button type="submit" class="btn1">提交</button>
								<?php echo $relink;?>
							</td>
						</tr>
					</tbody>
				</table>
				</form>
			</dd>
		</dl>
		<dl>
			<dt>管理标签</dt>
			<dd>
				<table class="plist">
					<tbody>
						<tr>
							<th width="20">ID</td>
							<th width="220">标签</td>
							<th width="46">操作</td>
						</tr>
					<?php
						$tagsDB->pagesize = 10;
						$page = $_GET['p'];
						foreach ($tagsDB->GetList("",$page) as $k => $v){
					?>
						<tr>
							<td style="font-size:11px;"><?php echo $k;?></td>
							<td><a href="#"><?php echo $v['tag'];?></a><br /></td>
							<td><a href="?m=tags&edit=<?php echo $v['id'];?>">编辑</a> | <a href="?m=tags&del=<?php echo $v['id'];?>" onclick="return confirm('确定删除?');">删除</a></td>
						</tr>
					<?php
						}
					?>
					</tbody>
					<tfoot>
						<tr>
							<td colspan="3" class="pagelist">
								<?php 
									$pagenum = ceil($tagsDB->listcount / $tagsDB->pagesize);
									echo "Page: ";
									for($p=1;$p<=$pagenum;$p++){
										echo "<a href=\"?m=tags&p=$p\">$p</a> ";
									}
									echo " Count: ".$tagsDB->listcount;
								?>
							</td>
						</tr>
					</tfoot>
				</table>
			</dd>
		</dl>

<?php
		}
	}
?>
	</div>
	<div class="footer">RunTime:<?php echo $runtime->time();?></div>
</div>
</body>
</html>
